﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HistogramPackage
{
    public class Bucket : CSVKit.CSVRow
    {
        private double _sample;
        private int _count;

        private static ArgumentOutOfRangeException NegativePop = new ArgumentOutOfRangeException("Count", "A bucket cannot have a negative population.");

        public double Sample
        { get { return _sample; } set { _sample = value; } }

        public int Count
        {
            get { return _count; }
            set
            {
                if (value < 0)
                    throw NegativePop;

                _count = value;
            }
        }

        public Bucket(double sample, int count = 1)
        {
            if (count < 0)
                throw NegativePop;
        }

        public Bucket(List<String> setRow)
        {
            this.setCSVRow(setRow);
        }

        public void setCSVRow(List<String> setRow)
        {
            this.Sample = Convert.ToDouble(setRow[0]);

            if (setRow.Count < 2 || !int.TryParse(setRow[1], out _count))
                this.Count = 1;
        }

        public List<String> getCSVRow()
        {
            List<string> csvRow = new List<String>();

            csvRow.Add(this.Sample.ToString());
            csvRow.Add(this.Count.ToString());

            return csvRow;
        }

        public static implicit operator Tuple<double, int>(Bucket bucket)
        {
            return new Tuple<double, int>(bucket.Sample, bucket.Count);
        }

        public static implicit operator KeyValuePair<double, int>(Bucket bucket)
        {
            return new KeyValuePair<double, int>(bucket.Sample, bucket.Count);
        }
    }
}
